package evolution.interceptor;

import java.io.IOException;

import evolution.algorithm.CoevolutionAlgorithm;
import evolution.individual.Individual;
import evolution.problem.OptimizationProblem;
import evolution.visualization.CoevolutionChart;
import evolution.visualization.FunctionAsMapChart;

public class ChartBuilderInterceptor extends SimpleIterationInterceptor {

	@Override
	public void finish(CoevolutionAlgorithm algorithm) {
		super.finish(algorithm);
		Individual[][] medianIndividuals = getRegistry().getMedianIndividuals();
		CoevolutionChart.createConvergenceChart("Mediana", medianIndividuals);

		Individual[][] bestIndividuals = getRegistry().getBestIndividuals();
		CoevolutionChart.createConvergenceChart("Najlepszy", bestIndividuals);

		OptimizationProblem optimizationProblem = algorithm
				.getEvolutionaryAlghorithmEpochRunners().get(0)
				.getEvolutionaryAlghorithm().getOptimizationProblem();

		if (optimizationProblem.getConstraints().getDimensions() == 2) {
			try {
				FunctionAsMapChart
						.createFunctionAsMapChart(optimizationProblem,
								getRegistry().getBestIndividuals());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
